package com.share;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.share.entity.Item;
import com.share.entity.User;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import lombok.Data;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.HtmlUtils;
import sun.misc.Unsafe;

import javax.print.DocFlavor;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
public class Test {

    static String  base = "string";


    static byte[] b2 = new byte[5*1024*1024];

//    static {
//        byte[] b2 = new byte[10*1024*1024];
//    }

//    final byte[] b2 = new byte[5*1024*1024];


    public static void main(String args[]) throws Throwable {


         ObjectMapper objectMapper = new ObjectMapper();

        String sunxj = objectMapper.writeValueAsString(new User().setNaem("sunxj").setAge(12));
        System.out.println(sunxj);


        System.out.println("------------------------------------------------------->finish");
    }

    public static void add (List<String> list) {
        list.add("a");

    }

    public static void handlerUser(User user) {
        user.setNaem(user.getNaem() + "1222");
    }

    private static void test11111() {
        String steps = "11、ABC2、DEF3、123";
        Pattern p = Pattern.compile("^\\d{1,}[、 || //.]");
        Matcher m = p.matcher(steps);

        String a = "1、   ";
//        String a = "1、去玩儿1、阿斯蒂芬";
        String s = a.replaceFirst("^\\d{1,}[、 || //.]", "");
        System.out.println("---->" + s);


        System.out.println(System.getProperty("line.separator"));
        System.out.println("askdfjalsdfCSVSADF".contains("CSV"));
    }

    public static void workingMemory(String a) {
        a = a + "123";

    }
    public static void workingMemory1(User user) {
        user.setNaem("sunxj");

    }
    public static void workingMemory1(Integer i) {
        i= i+1;

    }

    private static void test115() {
        String s = ",1,2,3,";
        String[] arr = s.split(",");
        System.out.println(arr.length);
        HashSet<String> set = new HashSet<>();


        for(int i=0; i<arr.length; i++) {
            if(org.apache.commons.lang3.StringUtils.isNotEmpty(arr[i])){
                set.add(arr[i]);
            }
        }
        System.out.println(set);
        String bugIds = org.apache.commons.lang3.StringUtils.join(set.toArray(), ",");
        System.out.println(bugIds);
    }

    private static void test1112() {
        HashSet<String> set = new HashSet<>();
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("c");
        for(String a: list) {
            System.out.println(a);
            set.add(a);
        }
        System.out.println(set);
        String[] s = set.toArray(new String[set.size()]);
        System.out.println(s);
        System.out.println(org.apache.commons.lang3.StringUtils.join(s, ","));
    }

    private static void test1111() {
        String[] arr1 = {"a", "b", "c", "d", "e", "f"};
        List<String> listA = new ArrayList<>(Arrays.asList(arr1));

        String[] arr2 = {"d", "e", "f", "g", "h"};
        List<String> listB = new ArrayList<>(Arrays.asList(arr2));

        Set<String> set = new HashSet<>(listA);
        set.addAll(listB);
        List<String> list = new ArrayList<>(set);
        System.out.println(list);

        List<String> collect = Stream.of(listA, listB)
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(collect);


        StringBuffer stringBuffer = new StringBuffer();
        for (int j= 0; j< collect.size(); j++) {
            stringBuffer.append(",");
            stringBuffer.append(collect.get(j));
        }
        stringBuffer.append(",");
        System.out.println(stringBuffer.toString());


        StringBuffer stringBuffer1 = new StringBuffer();
        ArrayList<String> strings = new ArrayList<>();
        for (String s : strings) {
            stringBuffer1.append(",");
            stringBuffer1.append(s);
        }
        stringBuffer1.append(",");
        System.out.println(stringBuffer1.toString());
    }

    private static void test345() {
        List<String> list = new ArrayList<String>();
        for (int i=0;i< Integer.MAX_VALUE;i++){
            String str = base + base;
            base = str;
            list.add(str.intern());
        }
    }

    private static void test234(String[] args, int i) throws Throwable {
        // -XX:PermSize=10M -XX:MaxPermSize=10M
        // -XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=10M
        // 注意：-XX:+UseCompressedClassPointers  开启后  -XX:CompressedClassSpaceSize=1G 才有效
        // -XX:CompressedClassSpaceSize=1M  Caused by: java.lang.OutOfMemoryError: Compressed class space

        while(true) {

            System.out.println(i++);

            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(User.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    return methodProxy.invokeSuper(objects, args);
                }
            });
            enhancer.create();

        }
    }

    private static void test112() {
        User user = new User();
        user.setAge(26);
        user.setNaem("SUNXJ");

        User user1 = new User();
        user1.setAge(1);
        user1.setNaem("xiaohema");

        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        users.add(user1);


        System.out.println(users);
        System.out.println(JSON.toJSONString(users));
        System.out.println(JSON.parseArray(JSON.toJSONString(users), User.class));
    }

    private static int getI() {
        return 10;
    }
    private static void put(Long c) {
        System.out.println(StringUtils.isEmpty(c));
    }

    private static void test111() {
        HashMap<Integer, List<User>> map = new HashMap<>();

        ArrayList<User> list1 = new ArrayList<>();
        ArrayList<User> list2 = new ArrayList<>();

        User a = new User("一",1);
        User b = new User("二",2);
        User c = new User("三",3);
        User d = new User("四",4);

        list1.add(a);
        list1.add(b);
        list2.add(c);
        list2.add(d);

        map.put(1,list1);
        map.put(2,list2);

        System.out.println(JSON.toJSONString(map));
    }

    private static void test11() throws Exception {
        testCallable(()->{
            System.out.println("123");
            return "123";
        });
    }

    private static void testCallable(Supplier<String> c) throws Exception {
        String call = c.get();
        System.out.println("call" + call);
    }

    String[] a;
    List<String> b;

    String gateway =  "http://api-gateway.jd.com:1602";
    public static final String GEN_SCOPE_CODE_URL = "/jacp-security/security/api/sso/user/sunxuejian/scope/sys/permission";

    private static void test10() {
        String s1 =  "http://api-gateway.JD.com:1602";

        String s2 = "/jacp-security/security/api/sso/user/sunxuejian/scope/sys/permission";

        String url = String.format("%s%s", s1, s2);
        System.out.println("url:" + url);
    }

    interface Predicate<T> {
        boolean test(T t);
    }

    public static boolean doPredicate(int age, Predicate<Integer> predicate) {
        return predicate.test(age);
    }


    static Runnable r = () -> System.out.println("do something.");

    public Runnable createRunnable() {
        return () -> {};
    }

    private static void test9() {
        Test test = new Test();
        System.out.println(test.getA());
        System.out.println(test.getB());
        System.out.println(test.a);
        System.out.println(test.b);
    }

    private static void test8() throws InterruptedException {
        System.out.println("当前线程："+ Thread.currentThread().getName());
        Thread.sleep(Integer.MAX_VALUE);
    }

    private static void test7() {
        String a = new String("12");
        m3(a);
        System.out.println("a:"+a);
    }

    private static void test6() {
        String a = "12";
        m3(a);
        System.out.println("a:"+a);
    }

    public static void m3(String n){
        System.out.println("n:"+n);
        n = n+"3";
        System.out.println("n:"+n);
    }

    private static void test5() {
        ArrayList<Integer> a = new ArrayList<>();
        a.add(1);
        a.add(2);
        m2(a);
        System.out.println("a:"+a);
    }

    public static void m2(ArrayList<Integer> n){
        System.out.println("n:"+n);
        n.add(3);
        System.out.println("n:"+n);
    }


    private static void test4() {
        Integer a = 1;
        //a作为值传入，并不是参数
        m1(a);
        System.out.println("a:"+ a);
    }

    public static void m1(int n){
        System.out.println("n:"+n);
        n = 2;
        System.out.println("n:"+n);
    }

    private static void test3() {
        String agentList = "33745f96399a47b0b53b290f2f1be4e1_11.18.82.193_0";
        String s = HtmlUtils.htmlUnescape(agentList);
        System.out.println("s:"+s);
        HashMap<String,String> agentMap = formatAgentMap(s);
        System.out.println("agentMap:"+agentMap);
    }

    private static HashMap<String,String> formatAgentMap(String agentList){
        HashMap<String,String> agentMap  = new HashMap<>();
        if (!"".equals(agentList)) {
            String[] agentArray = agentList.split("&");
            for(String agent:agentArray)
                if (agent.indexOf("_") >= 0) {
                    agentMap.put(agent.split("_")[0].trim(),agent.split("_")[1].trim());
                }
        }
        return agentMap;
    }

    private static void test2() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            Date birthday = sdf.parse("1994年02月03日");
            System.out.println("##"+birthday);
            String aa = sdf.format(birthday);
            System.out.println(aa);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void test1() {
        String aa = "8@12&222000@333000&0@0&2@3";
        if (aa != null && aa.contains("&")) {
            String[] aas = aa.split("&");
            String[] p_vuserNum = aas[0].split("@");
            String[] p_duration = aas[1].split("@");
            String[] p_execType = aas[2].split("@");
            String[] p_agentNum = aas[3].split("@");
        }
    }
}
